﻿using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.Revit.DB;
using YArchitech.Revit.RevitExtension;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Kits
{
	public class TableDrawingKitForPicture
	{
		public List<CurveInfo> Curves
		{
			get
			{
				return this.m_Curves;
			}
		}

		public List<TextNoteInfo> Texts
		{
			get
			{
				return this.m_Texts;
			}
		}

		public double FontHeight
		{
			get
			{
				return this.m_FontHeight;
			}
		}

		public TableDrawingKitForPicture(TableHead tableHead, double fontHeight)
		{
			this.m_TableHead = tableHead;
			this.m_FontHeight = fontHeight;
		}

		public void Drawing()
		{
			try
			{
				this.DrawTableHeaderByInfos();
			}
			catch (Exception)
			{
			}
		}

		private void DrawTableHeaderByInfos()
		{
			foreach (TableHeadCell tableHeadCell in this.m_TableHead.TableHeadInfos)
			{
				double num = 0.0;
				double num2 = 0.0;
				this.m_TableHead.CellPosition(tableHeadCell, ref num, ref num2);
				XYZ xyz = new XYZ(num, -num2, 0.0);
				double num3 = this.m_TableHead.CellWidth(tableHeadCell);
				double num4 = this.m_TableHead.CellHeight(tableHeadCell);
				if (!Geometry.IsEqual(num3, 0.0) && !Geometry.IsEqual(num4, 0.0))
				{
					XYZ xyz2 = xyz + XYZ.BasisX * num3;
					XYZ xyz3 = xyz - XYZ.BasisY * num4;
					XYZ xyz4 = xyz3 + XYZ.BasisX * num3;
					this.DrawModelLine(xyz, xyz2);
					this.DrawModelLine(xyz2, xyz4);
					this.DrawModelLine(xyz4, xyz3);
					this.DrawModelLine(xyz3, xyz);
					this.DrawText(xyz, num3, num4, tableHeadCell.Value);
				}
			}
		}

		private int GetLength(string str)
		{
			if (str.Length == 0)
			{
				return 0;
			}
			Encoding encoding = new ASCIIEncoding();
			int num = 0;
			byte[] bytes = encoding.GetBytes(str);
			for (int i = 0; i < bytes.Length; i++)
			{
				if (bytes[i] == 63)
				{
					num += 2;
				}
				else
				{
					num++;
				}
			}
			return num;
		}

		private void DrawText(XYZ ptLeftTop, double width, double height, string text)
		{
			XYZ center = new XYZ(ptLeftTop.X + width / 2.0, ptLeftTop.Y - height / 2.0, 0.0);
			TextNoteInfo item = new TextNoteInfo(text, PenColor.PC_Black, this.m_FontHeight, center, width, height);
			this.m_Texts.Add(item);
		}

		private void DrawModelLine(XYZ ptStart, XYZ ptEnd)
		{
			Line curve = YJKLineEx.YJKGetBound(ptStart, ptEnd);
			CurveInfo item = new CurveInfo(curve, PenColor.PC_Black, 1f);
			this.m_Curves.Add(item);
		}

		private double HeadWidth()
		{
			double num = 0.0;
			foreach (double num2 in this.m_TableHead.GetCellWidthList())
			{
				num += num2;
			}
			return num;
		}

		private double HeadHeight()
		{
			double num = 0.0;
			foreach (double num2 in this.m_TableHead.GetCellHeightList())
			{
				num += num2;
			}
			return num;
		}

		private TableHead m_TableHead;

		private double m_FontHeight;

		private XYZ m_ptLeftTop = new XYZ(0.0, 0.0, 0.0);

		private List<CurveInfo> m_Curves = new List<CurveInfo>();

		private List<TextNoteInfo> m_Texts = new List<TextNoteInfo>();
	}
}
